//
//  YYMusicChildMeetController.swift
//  STMusic
//
//  Created by stoneobs on 2017/6/12.
//  Copyright © 2017年 stoneobs. All rights reserved.
// //**********************遇见***************************

import UIKit

class YYMusicChildMeetController: STTableViewController {

    var page = 1;
    
    var dataSouce:Array<YYMeetModel> = [];
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.height = YYMusicArticalViewController.scrollViewHeight;
        self.tableView.separatorStyle = .none;
        self.tableView.backgroundColor = UIColor.white.withAlphaComponent(0.7);
        self.view.backgroundColor = .clear;
        self.initSbviews();
        self.loadNetworkRequest();
        
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func initSbviews() -> Void {
        //刷新
        weak var weakSelf = self;
        let header:MJRefreshGifHeader = MJRefreshGifHeader.init(refreshingBlock: {
            weakSelf?.page = 1;
            weakSelf?.dataSouce = [];
            weakSelf?.loadNetworkRequest();
            
        });
        header.lastUpdatedTimeLabel.textColor = FirstTextColor;
        header.stateLabel.textColor = FirstTextColor;
        self.tableView.mj_header = header;
        
        
        
        
        
        
    }
    // MARK: UITableView DataSouce
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSouce.count;
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return YYMusicMeetTableViewCell.cellHeight;
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.0001;
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:YYMusicMeetTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "cell") as? YYMusicMeetTableViewCell;
        if cell == nil {
            cell = YYMusicMeetTableViewCell.init(style: .default, reuseIdentifier: "cell");
        }
        cell?.selectionStyle = .none;
        let cellmodel = self.dataSouce[indexPath.row];
        cellmodel.indexString = String(indexPath.row + 1);
        cell?.model = cellmodel;
        return cell!;
    }
    
    // MARK: UITableView Delegate
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false);
        let  vc = YYMeetDetailViewController.init();
        vc?.model = self.dataSouce[indexPath.row];
        self.navigationController?.pushViewController(vc!, animated: true);
        
    }
    
    func loadNetworkRequest() -> Void {
        let time:Int = Int(NSDate().timeIntervalSince1970);
        
        let str = "http://www.wawa.fm:9090/wawa/api.php/user/meetList/r/30/page/\(self.page)/sign=479338c31e4b63107c4ae76972ba5cb15ea95cda7be26ddb0183258d8298e926&api_key=08b1e567157582019f7fe639c841c42a&timestrap=\(time)"
        YYYuYinNetWorkManger.defultManger.getNetwork(urlString: str, dic: [:], isJson: true, successHandle: { (json) in
            
            print("json = \(json)");
            
            var isUpdate = false;
            if let array:Array<Dictionary<String,Any>> = json as? Array{
                
                for objectdic in array{
                    
                    let model:YYMeetModel = YYMeetModel.mj_object(withKeyValues: objectdic);
                    isUpdate = true;
                    self.dataSouce.append(model);
                    
                }
                
            }
            
            if self.dataSouce.count > 9 && (self.tableView.mj_footer == nil ){
                weak var weakSelf = self;
                self.tableView.mj_footer = MJRefreshAutoGifFooter.init(refreshingBlock: {
                    weakSelf?.page  = self.page + 1;
                    weakSelf?.loadNetworkRequest();
                });
            }
            
            
            
            self.tableView.mj_header?.endRefreshing();
            self.tableView.mj_footer?.endRefreshing();

            if isUpdate == false {
                self.tableView.mj_footer?.endRefreshingWithNoMoreData();
            }
            self.tableView.reloadData();
            
            
        }) { (str) in
            self.tableView.mj_header.endRefreshing();
            self.tableView.mj_footer?.endRefreshing();
        };
    }

}
